package com.lsx143.gmall_logger.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class LoggerController {
    @Autowired
    @SuppressWarnings("all")
    private KafkaTemplate<String, String> kafka;

    @RequestMapping("/applog")
    public String doLog(@RequestParam("param") String logString) {
        //1、日志落写到磁盘
        saveToDisk(logString);
        //2、日志输出到kafka
        saveToKafka(logString);
        return logString;
    }

    /**
     * 日志落写到磁盘
     *
     * @param logString 日志数据
     */
    private void saveToDisk(String logString) {
        log.info(logString);
    }

    /**
     * 日志落写到Kafka
     *
     * @param logString 日志数据
     */
    private void saveToKafka(String logString) {
        kafka.send("ods_log", logString);
    }
}
